<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=9">
<meta name="generator" content="Doxygen 1.8.13">
<title>OpenCV:将OpenCV与gcc和CMake一起使用</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon">
<link href="../../tabs.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css">
<link href="../../stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../df/d65/tutorial_table_of_content_introduction.html">Introduction to OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">结合gcc和CMake使用OpenCV</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>上一个教程:</b> <a class="el" href="../../d7/d9f/tutorial_linux_install.html">Installation in Linux</a></p>
<p><b>下一个教程:</b> <a class="el" href="../../d7/d16/tutorial_linux_eclipse.html">Using OpenCV with Eclipse (plugin CDT)</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">原作者</td><td align="left">Ana Huamán</td></tr>
<tr>
<td align="right">兼容性</td><td align="left">OpenCV &gt;= 3.0</td></tr>
</table>
<dl class="section warning"><dt>警告</dt><dd>本教程可能包含过时的信息:</dd></dl>
<dl class="section note"><dt>Note</dt><dd>我们假设您已经在工作站中成功安装了OpenCV:</dd></dl>
<ul>
<li>在代码中使用OpenCV最简单的方法是<a href="http://www.cmake.org/">CMake</a>. 一些优点(来自Wiki):<ol type="1">
<li>在Linux和Windows之间进行移植时不需要做任何更改</li>
<li>可以通过CMake轻松地与其他工具(即Qt、ITK和VTK)结合使用</li>
</ol>
</li>
<li>如果您不熟悉CMake,请看这里<a href="http://www.cmake.org/cmake/help/cmake_tutorial.html">CMake教程</a>在它的网站上:</li>
</ul>
<h2>步骤</h2>
<h3>使用OpenCV创建程序</h3>
<p>让我们使用一个简单的程序,如下面显示的DisplayImage.cpp.</p><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;stdio.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;opencv2/opencv.hpp&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">    <span class="keywordflow">if</span> ( argc != 2 )</div><div class="line">    {</div><div class="line">        printf(<span class="stringliteral">&quot;usage: DisplayImage.out &lt;Image_Path&gt;\n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> image;</div><div class="line">    image = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( argv[1], 1 );</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ( !image.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4d33bed1c850265370d2af0ff02e1564">data</a> )</div><div class="line">    {</div><div class="line">        printf(<span class="stringliteral">&quot;No image data \n&quot;</span>);</div><div class="line">        <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(<span class="stringliteral">&quot;Display Image&quot;</span>, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> );</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;Display Image&quot;</span>, image);</div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0);</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> <h3>创建CMake文件</h3>
<p>现在您必须创建CMakeLists.txt文件.应该是这样的:</p><div class="fragment"><div class="line">cmake_minimum_required(VERSION 2.8)</div><div class="line">project( DisplayImage )</div><div class="line">find_package( OpenCV REQUIRED )</div><div class="line">include_directories( ${OpenCV_INCLUDE_DIRS} )</div><div class="line">add_executable( DisplayImage DisplayImage.cpp )</div><div class="line">target_link_libraries( DisplayImage ${OpenCV_LIBS} )</div></div><!-- fragment --> <h3>生成可执行文件</h3>
<p>这一部分很简单,只需像使用CMake的任何其他项目一样进行即可:</p><div class="fragment"><div class="line">cd &lt;DisplayImage_directory&gt;</div><div class="line">cmake .</div><div class="line">make</div></div><!-- fragment --> <h3>结果</h3>
<p>现在您应该已经有了一个可执行文件(在本例中称为DisplayImage):您只需运行它,给出一个图像位置作为参数,即:</p><div class="fragment"><div class="line">./DisplayImage lena.jpg</div></div><!-- fragment --><p>你应该得到一个很好的窗口,如下图所示:</p>
<div class="image">
<img src="../../GCC_CMake_Example_Tutorial.jpg" alt="GCC_CMake_Example_Tutorial.jpg">
</div>
 </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"><address class="footer"><small>Generated on Fri Apr 2 2021 11:36:36 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen">
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
